<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class type
{
	public $table;
	public $pages;
	public $db;
	public $error;
	public $current_user;

	function type()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'type';
        $this->current_user = $current_user;
	}

	function get_top_types()
	{
		$list = $this->db->query('select * from '.$this->table.' where topcode is null or topcode="" order by listorder desc,inputtime desc');
		return $list;
	}

	function get_typs($top_code,$parentcode='')
	{
		if(empty($parentcode))
		{
			$this->db->prepare('select * from '.$this->table.' where topcode=? order by listorder desc, inputtime desc');
			$this->db->set_string($top_code);
		}
		else
		{
			$this->db->prepare('select * from '.$this->table.' where topcode=? and parentcode=? order by listorder desc, inputtime desc');
			$this->db->set_string($top_code);
			$this->db->set_string($parentcode);
		}
		$res = $this->db->execute();
		$list = array();
		if(is_array($res))
		{
			foreach ($res as $r)
			{
				$list[$r['tcode']] = $r;
			}
		}
		return $list;
	}
/*
 *  所有分类
 */
       function alltype()
        {
                $this->db->prepare("select * from edufe_type");
                $options = $this->db->execute();
                return $options;
        }
/*
 * select控件 生成树
 */
        function select_options($options = '', $parentcode = '', $name = '', $alt = '', $id ='', $property = '')
	{
		global $tree;
		if(!is_object($tree))
		{
			require_once 'tree.class.php';
			$tree = new tree;
		}
		if(!$id) $id = $name;
		$data = "<select name='$name' id='$id' $property>\n<option value='' \$selected>$alt</option>\n";
		if(is_array($options))
		{
			$categorys = array();
			foreach($options as $option)
			{
                            $categorys[] = array('id'=>$option['tcode'], 'parentid'=>$option['parentcode'], 'name'=>$option['tname']);
			}
			$tree->tree($categorys);
			$data .= $tree->get_tree($parentcode, "<option value='\$id' >\$spacer\$name</option>\n");
		}
		$data .= '</select>';
		return $data;
	}


        function get_one($where = '')
        {       if(!empty($where))
                    {
                        $where =  " where ".$where;
                    }
                else
                    {
                        return false;
                    }
                $this->db->prepare("select * from edufe_type ".$where);
                $res = $this->db->execute(true);
                return $res;
        }

/*
 * 添加一个分类
 */
        function add($type)
        {
            $uuid = $this->db->get_uuid();
            $this->db->prepare("insert into ".$this->table." (tid, tcode, tname, parentcode, inputtime, topcode) values (?, ?, ?, ?, ?, ?)");
            $this->db->set_string($uuid);
            $this->db->set_string($type['code']);
            $this->db->set_string($type['name']);
            $this->db->set_string($type['pcode']);
            $this->db->set_string($type['inputtime']);
            $this->db->set_string($type['topcode']);
            $res = $this->db->execute();
            return $res;
        }

/*
 *  显示分类信息
 */
        function list_info($page, $pagesize, $parentcode = '')
        {
            $this->db->prepare("select * from ".$this->table." where parentcode = ? order by listorder desc limit ?,?");
            $this->db->set_string($parentcode);
            $this->db->set_int(($page-1)*$pagesize);
            $this->db->set_int($pagesize);
            $list_res = $this->db->execute();
            return $list_res;
        }
/*
 * 修改分类
 */
        function edit($type = array(), $tid = '')
        {
            $this->db->prepare("update ".$this->table." set tcode = ?, tname = ?, parentcode = ?, topcode = ? where tid = ?");
            $this->db->set_string($type['code']);
            $this->db->set_string($type['name']);
            $this->db->set_string($type['pcode']);
            $this->db->set_string($type['topcode']);
            $this->db->set_string($tid);
            $res = $this->db->execute();
            return $res;
        }
/*
 * 删除分类
 */
        function delete($tid)
        {
            $this->db->prepare('delete from edufe_type where tid=?');
            $this->db->set_string($tid);
            $res = $this->db->execute();
            return $res;
        }
/*
 *  统计分类总数
 */
        function count($parentcode = '')
        {
            $this->db->prepare("select count(*) as total from ".$this->table." where parentcode = ?");
            $this->db->set_string($parentcode);
            $count = $this->db->execute(TRUE);
            return $count;
        }
/*
 * 排序
 */
        function listorder($info)
	{
            if(!is_array($info)) return false;
            foreach($info as $tid=>$listorder)
            {
                    $id = intval($tid);
                    $listorder = intval($listorder);
                    $this->db->prepare("update ".$this->table." set listorder = ? where tid = ?");
                    $this->db->set_int($listorder);
                    $this->db->set_string($tid);
                    $this->db->execute();
            }
            return true;
	}
/*
 *  更改子分类的topcode $fisetcode 为原分类的编码 $tcode 为修改后的编码
 */
        function change_chl_topcode($firstcode,$tcode,$topcode)
        {
            if($topcode == '')
            {
                $topcode = $tcode;
            }
            if($firstcode == $tcode)
            {

                $this->db->prepare("select tcode from ".$this->table." where parentcode = ?");
                $this->db->set_string($tcode);
                $res = $this->db->execute();
                if($res != '')
                {
                   $this->get_chl_topcode($res, $topcode);
                }
                else
                {
                 return false;
                }
            }
            else
            {
                $this->db->prepare("select tcode from ".$this->table." where parentcode = ?");
                $this->db->set_string($firstcode);
                $res = $this->db->execute();
                if($res != '')
                {
//父类code更改先修改父类code 在更改topcode
                    for($r=0;$r<sizeof($res);$r++)
                    {
                        $this->db->query("update ".$this->table." set parentcode ='".$tcode."' where tcode = '".$res[$r]['tcode']."'");
                    }
                    $this->get_chl_topcode($res, $topcode);
                }
                else
                {
                    return false;
                }
            }
         }
        function get_chl_topcode($tcode, $topcode)
        {
            for($i=0;$i<sizeof($tcode);$i++)
            {
                $this->db->query("update ".$this->table." set topcode = '".$topcode."' where tcode = '".$tcode[$i]['tcode']."'");
                $this->db->prepare("select tcode from ".$this->table." where parentcode = ?");
                $this->db->set_string($tcode[$i]['tcode']);
                $res = $this->db->execute();
                if($res != '')
              {
               $this->get_chl_topcode($res, $topcode);
              }
            }
            return true;
        }
//删除分类及子分类
        function delete_types($tcode)
        {
            $this->db->prepare("select * from ".$this->table." where parentcode = ? ");
            $this->db->set_string($tcode);
            $datas = $this->db->execute();
            foreach($datas as $one)
            {
                $chl_tcode = $one['tcode'];
                $this->delete_types($chl_tcode);
            }
            $this->db->prepare('delete from edufe_type where tcode=?');
            $this->db->set_string($tcode);
            $res = $this->db->execute();
            return $res;
        }
}
?>
